/*jslint browser: true, devel: true, bitwise: true, white: true, plusplus: true */
/*global $:true, define:true */

/**
 * @module UI
 */

define(['WID/UI/Widget','WID/Utility/Data/LinkedList'],
function(Widget,LinkedList,Utility) {
"use strict";

/** 
 * Kontener na inne kontrolki.
 * @class Container
 * @constructor
 * @extends Widget
 * @param {Object} props Parametry dla kontenera. Zobacz też dostępne w {{#crossLink "Widget"}}{{/crossLink}}
 *  @param {Array} props.childs Tablica kontrolek, które będą dziećmi tego kontenera.
 *  @param {Number} props.z Wartość dla zindex dla kontenera.
 */
function Container(props) {
	props = props||{};
	Widget.call(this,props,'div');
	this.Childs = new LinkedList();
	
	if (props.childs) {
		this.addChilds(props.childs);
	}
	
	this.ntv.style.zindex = props.z||'';
	
} var extend = Object.create(Widget.prototype); extend.constructor = Container;

extend.getChild = function(childID) {
	return Utility.find(this.Childs,function(element) {
		return (element.id() === childID);
	});
};

extend.foreach = function(each) {
	Utility.foreach(this.Childs,each);
};
	
extend.addChild = function(child) {
	if (child.Parent) {
		child.Parent.removeChild(child);
	}
		
	child.node = this.Childs.append(child);
	child.Parent = this;
	$(this.ntv).append(child.ntv);
};
	
extend.addChilds = function(childs) {
	var i, length = childs.length;
	for (i = 0; i < length;++i) {
		this.addChild(childs[i]);
	}
};
	
extend.removeChild = function(child) {
	if (child.Parent === this) {
		this.Childs.remove(child.node);
		child.node = undefined;
		child.Parent = undefined;
		$(child.ntv).remove();
	} else {
		throw new Error('[Conteiner] Child hasnt this Parent');
	}
};
	
extend.removeChilds = function(childs) {
	var i, length = childs.length;
	for (i = 0; i < length;++i) {
		this.removeChild(childs[i]);
	}
};
Container.prototype = extend;
return Container;
});
